<?php include ($_SERVER['DOCUMENT_ROOT'].'/insp-modules/misc/config.php');?>
<?php
	$conn = mysql_connect($host, $user, $password) or die(mysql_error());
	mysql_select_db("travel");
	$sql = "SELECT * FROM address";
	$result = mysql_query($sql, $conn) or die(mysql_error());
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" />
	<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
	
	<link rel="stylesheet" href="/etronix/css/style.css" />
	
	<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="http://code.google.com/apis/gears/gears_init.js"></script> 
	<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&libraries=geometry"></script>
	
	<script type="text/javascript">
	
		var initialLocation;
		var browserSupportFlag =  new Boolean();
		var map;
	  
		var myLoc = new Array();
		var message = new Array();
		var info = new Array();
		
<?php
	$n = 0;
	while ($row = mysql_fetch_assoc($result)) {
echo <<< HERE

		info[$n] = new Array();
		info[$n][0] = "$row[id]";
		info[$n][1] = "$row[address]";
		info[$n][2] = "$row[city]";
		info[$n][3] = "$row[state]";
		info[$n][4] = "$row[zipcode]";
		info[$n][5] = "$row[phone]";
		info[$n][8] = "$row[weekday]";
		info[$n][9] = "$row[sat]";
		info[$n][10] = "$row[sun]";
		
HERE;
		echo "myLoc[$n] = new google.maps.LatLng($row[lat],$row[lng]);"; 
		$n++;
	}
	$loc = $_REQUEST['location'];	

echo <<< HERE
		
		var address = "$loc";
		var geocoder;
		var lat_temp;
		var lng_temp;

function initialize() {
  // Try W3C Geolocation method (Preferred)
  if(navigator.geolocation) {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
		if (address == "") {
			initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
			var distance = new Array();
			var temp = new Array();
		
			for (var i = 0; i < $n; i++) {
				distance[i] = google.maps.geometry.spherical.computeDistanceBetween(myLoc[i], initialLocation);
				distance[i] = distance[i]/1000*1.54;
				distance[i] = distance[i].toFixed(1);
				temp[i] = distance[i];
				message[i] = distance[i].toString() + " mi";
			}
		
			temp.sort(function(a,b){return a-b;});
			var alertMessage = new Array();
			
			var cont_h = document.getElementById("Content");
			var l_h = "<div><h2>Locations close to your current location</h2></div><hr />";
			var oDiv_h = document.createElement('div');
			oDiv_h.innerHTML = l_h;
			cont_h.appendChild(oDiv_h);
			
			for (var j = 0; j < 4; j++) {
				for (var k = 0; k < $n; k++) {
					if (temp[j]==distance[k]) {
						var cont = document.getElementById("Content");
						var l;
						l = "<div class='loclist'><a href='#' id='" + k + "' onclick='show(this.id)'><b>" + info[k][2] + ",  " + info[k][3] 
							+ "</b></a><br/>"+ info[k][1] + ", " + info[k][2] + ", " + info[k][3] + ", " + info[k][4] + "<br/>" 
							+ "<a href='tel:" + info[k][5] + "' target='_self'>" + info[k][5] + "</a>" + "<br/>" + message[k] + "<br/>"
							+ "<button onclick='tel:" + info[k][5] + "' target='_self' class='button-secondary'>Call</button>"
							+ "<button onclick=\"window.location.href='current.php?startlat=" + initialLocation.lat() + "&startlng=" + initialLocation.lng() 
							+ "&endlat=" + myLoc[k].lat() + "&endlng=" + myLoc[k].lng() 
							+ "&addr=" + info[k][2] 
							+ "'\" class='button-secondary'>Map</button>"
							+ "<button onclick=\"window.location.href='direction.php?startlat=" + initialLocation.lat() + "&startlng=" + initialLocation.lng()
							+ "&endlat=" + myLoc[k].lat() + "&endlng=" + myLoc[k].lng()
							+ "&addr=" + info[k][2]
							+ "'\" class='button-secondary'>Direction</button>"
							+ "<br/></div><hr />";
						var oDiv = document.createElement('div');
						oDiv.innerHTML = l;
						cont.appendChild(oDiv);
					}
				}
			}
		} else {
			geocoder = new google.maps.Geocoder();
			geocoder.geocode( { 'address': address}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					
					lat = results[0].geometry.location.lat();
					lng = results[0].geometry.location.lng(); 
					initialLocation = new google.maps.LatLng(lat, lng);
					var distance = new Array();
					var temp = new Array();
		
					for (var i = 0; i < $n; i++) {
						distance[i] = google.maps.geometry.spherical.computeDistanceBetween(myLoc[i], initialLocation);
						distance[i] = distance[i]/(1000*1.61);
						distance[i] = distance[i].toFixed(1);
						temp[i] = distance[i];
						message[i] = distance[i].toString() + " mi";
			
					}
		
					temp.sort(function(a,b){return a-b;});
					var alertMessage = new Array();
		
					var cont_h = document.getElementById("Content");
					var l_h = "<div><h2>Locations close to '" + address + "'</h2></div>";
					var oDiv_h = document.createElement('div');
					oDiv_h.innerHTML = l_h;
					cont_h.appendChild(oDiv_h);
					
					for (var j = 0; j < 4; j++) {
						for (var k = 0; k < $n; k++) {
							if (temp[j]==distance[k]) {
								var cont = document.getElementById("Content");
								var l;
								l = "<div class='loclist'><a href='#' id='" + k + "' onclick='show(this.id)'><b>" + info[k][2] + ",  " + info[k][3] 
									+ "</b></a><br/>"+ info[k][1] + ", " + info[k][2] + ", " + info[k][3] + ", " + info[k][4] + "<br/>" 
									+ "<a href='tel:" + info[k][5] + "' target='_self'>" + info[k][5] + "</a>" + "<br/>" + message[k] + "<br/>"
									+ "<button onclick='tel:" + info[k][5] + "' target='_self' class='button-secondary'>Call</button></li>"
									+ "<button onclick=\"window.location.href='current.php?startlat=" + initialLocation.lat() + "&startlng=" + initialLocation.lng() 
									+ "&endlat=" + myLoc[k].lat() + "&endlng=" + myLoc[k].lng() 
									+ "&addr=" + info[k][2] 
									+ "'\" class='button-secondary'>Map</button>"
									+ "<button onclick=\"window.location.href='direction.php?startlat=" + initialLocation.lat() + "&startlng=" + initialLocation.lng()
									+ "&endlat=" + myLoc[k].lat() + "&endlng=" + myLoc[k].lng()
									+ "&addr=" + info[k][2]
									+ "'\" class='button-secondary'>Direction</button>"
									+ "<br/></div><hr />";
								var oDiv = document.createElement('div');
								oDiv.innerHTML = l;
								cont.appendChild(oDiv);
							}
						}
					}	
				} else {
					alert("Geocode was not successful for the following reason: " + status);
				}
			});
		}
		
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  } else if (google.gears) {
    // Try Google Gears Geolocation
    browserSupportFlag = true;
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  } else {
    // Browser doesn't support Geolocation
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }
  
}
HERE;
?>
// shows the alert of whole detail
function show(k) {
	alert("Address: " + info[k][1] + ", " + info[k][2] + ", " + info[k][3] + ", " + info[k][4] + "\nPhone: " + info[k][5] + "\nOffice Hour:\n"
		+ "Mon - Fri : " + info[k][8] + "\nSat :            " + info[k][9] + "\nSun :           " + info[k][10]);
	/*var light = document.getElementById("light");
	var ele;
	var c = "";
	c += "<div><p>Address: " + info[k][1] + ", " + info[k][2] + ", " + info[k][3] + ", " + info[k][4] + "</p>";
	c += "<p>Phone: <a href=\"tel:(732)881-2713\" target=\"_self\">(732)881-2713</a></p></div>";
	ele = document.createElement('div');
	ele.innerHTML = c;
	light.appendChild(ele);
	light.style.display='block';*/
	
}

function handleNoGeolocation(errorFlag) {
  if (errorFlag == true) {
    initialLocation = newyork;
    contentString = "Error: The Geolocation service failed.";
  } else {
    initialLocation = siberia;
    contentString = "Error: Your browser doesn't support geolocation. Are you in Siberia?";
  }
  map.setCenter(initialLocation);
  infowindow.setContent(contentString);
  infowindow.setPosition(initialLocation);
  infowindow.open(map);
}
	</script>
</head>
<body onload="initialize()">
	<div id="wrapper">			
		<?php include 'header.php'?>
		
		<div id="Content" >
		
		</div>
		
		<?php include 'footer.php'?>
	</div>
</body>
</html>

